<?php
/**
 * CommonMsg
 *  
 * @author Administrator
 * @version 
 */
class Model_CommonMsg extends Zend_Db_Table_Abstract
{
    /**
     * The default table name
     */
    protected $_name = 'tcl_common_msg';
    
    //消息列表
    public function getMsgList($page, $url, $uid, $count=40) {
    	
        $core = new Model_Core();
        $db = $this->getAdapter();
        $select = $db->select();
        
        $select->from($this->_name, array('id','to_uid','title','is_read','created_at'));
        $select->where('to_uid = ?', $uid);
        $select->order(array('is_read DESC','created_at DESC'));
        
     	$how = count($db->fetchAll($select));
        //echo $how;
        //print_r($db->fetchAll($select));
        
        $howpage = ceil($how / $count);
        $offset	 = $page * $count - $count;

        $multipage = $core->html_multi($page, $howpage, $url);
		
        $select->limit($count, $offset);
        
        try {
            $row = $db->fetchAll($select);
        } catch (Exception $e) {
            echo $e;
            exit();
        }
        
        $results = array(
        		'row' => $row,
        		'mulitpage' => $multipage,
        );
        return $results;
    }
    
    //取详细 CONTENT
    public function getMsgContent($id) {
    	
        $db = $this->getAdapter();
        try {
        	$row = $this->find($id)->toArray();
        } catch (Exception $e) {
        	echo $e;
        	exit();
        }
        
        return $row[0];
    }
    
    
    //设置已读
    public function setMsgIsread($ids) {
        
    	$db = $this->getAdapter();
    
    	$set = array(
    		'is_read'   => 0,
    	);
    	$ids = explode(',', $ids);
    	
    	$where = $db->quoteInto('id IN (?)', $ids);    
    	try {
    		$row = $this->update($set, $where);
    	} catch (Exception $e) {
    		echo $e;
    		exit();
    	}
    	return $row;
    }
    
    //删除
    public function setMsgDel($ids, $uid) {
    
    	$db = $this->getAdapter();
    
    	 
    	$where = $db->quoteInto('id IN (?)', $ids);
    	//.$db->quoteInto("AND uid = ?", $uid);
    
    	try {
    		$row = $this->delete($where);
    	} catch (Exception $e) {
    		echo $e;
    		exit();
    	}
    	return $row;
    }
    
    //是否有未读消息
    public function getIsMsg($uid) {
    
        $db = $this->getAdapter();

    	$where = $db->quoteInto('to_uid = ?', $uid)
    			.$db->quoteInto("AND is_read = ?", 1);
    
    	try {
    		$row = $this->fetchAll($where)->count();
    	} catch (Exception $e) {
    		echo $e;
    		exit();
    	}
        
    	
        return $row;
    }
    
    //发送消息
    public function setMsgTo($to_uid, $title, $content) {
    	
        $data = array(
        	'to_uid' => $to_uid,
        	'title' => $title,
        	'content' => $content,
        	'is_read' => 1,
        	'created_at' => time(),
        );
        
        try {
        	$id = $this->insert($data);
        } catch(Exception $e) {
        	// when it fails to insert
        	// how can i run this echo
        	echo $e; //??
        	exit();
        }
        return $id;
    }
    
}
